<template>
  <div class="app-container">
    <el-form
      ref="form"
      :inline="true"
      :model="formData"
      class="demo-form-inline"
    >
      <el-form-item label="客户名称" prop="customerName">
        <el-input
          v-model="formData.customerName"
          placeholder="请输入客户名称"
        ></el-input>
      </el-form-item>

      <el-form-item label="客户手机号" prop="customerPhone">
        <el-input
          v-model="formData.customerPhone"
          placeholder="请输入客户手机号"
        ></el-input>
      </el-form-item>

      <el-form-item label="优惠券">
        <el-select
          clearable
          v-model="formData.couponId"
          placeholder="请选择结算状态"
        >
          <el-option
            v-for="item in couponList"
            :label="item.name"
            :value="item.id"
            :key="item.id"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="状态">
        <el-select clearable v-model="formData.status" placeholder="请选择状态">
          <el-option
            v-for="item in userCouponStatusList"
            :key="item.value"
            :label="item.name"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onSearch">搜索</el-button>
        <el-button type="primary" @click="reset">重置</el-button>
      </el-form-item>
    </el-form>

    <el-button class="create" type="primary" @click="create">创建</el-button>

    <el-table :data="list" style="width: 100%">
      <el-table-column prop="id" label="ID" width="180"> </el-table-column>
      <el-table-column prop="customerName" label="领取用户" width="180">
      </el-table-column>
      <el-table-column prop="customerPhone" label="手机号" width="180">
      </el-table-column>
      <el-table-column prop="name" label="优惠券名称" width="180">
      </el-table-column>
      <el-table-column prop="amount" label="金额" width="180">
        <template slot-scope="scope">
          {{ Number(scope.row.amount).toFixed(2) }}（元）
        </template>
      </el-table-column>
      <!-- <el-table-column prop="fullMoney" label="满减金额">
        <template slot-scope="scope">
          {{ Number(scope.row.fullMoney).toFixed(2) }}（元）
        </template>
      </el-table-column> -->
      <el-table-column prop="status" label="状态">
        <template slot-scope="scope">
          {{ userCouponStatusMap[scope.row.status] }}
        </template>
      </el-table-column>
      <el-table-column prop="updateTime" label="领取时间" width="180" />
      <el-table-column prop="userTime" label="使用时间" width="180">
      </el-table-column>
      <el-table-column label="操作" width="180">
        <template slot-scope="scope">
          <el-popconfirm
            style="margin-left: 10px"
            title="您确定要删除当前优惠券吗？"
            @confirm="deleteItem(scope.row.id)"
          >
            <el-button type="text" size="small" slot="reference"
              >删除</el-button
            >
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="queryParams.total > 0"
      :total="queryParams.total"
      :page.sync="queryParams.page"
      :limit.sync="queryParams.pageSize"
      @pagination="getListData"
    />
  </div>
</template>

<script>
import {
  deleteUserCoupon,
  addOrEditCoupon,
  getCouponList,
  getUserCouponList,
} from "@/api/coupon/index";
import { userCouponStatusMap, userCouponStatusList } from "@/config/index";

export default {
  name: "Index",
  data() {
    return {
      queryParams: {
        page: 0,
        limit: 20,
        total: 0,
      },
      formData: {
        customerName: "",
        customerPhone: "",
        couponId: "",
        status: "",
      },
      couponList: [],
      list: [],
      userCouponStatusMap,
      userCouponStatusList,
    };
  },
  created() {
    this.initData();
    this.getListData({
      page: 1,
      limit: 20,
    });
  },
  methods: {
    initData() {
      getCouponList({}, 1, 9999).then(({ data }) => {
        this.couponList = data.records;
      });
    },
    getListData({ page, limit }) {
      getUserCouponList(
        this.queryParams,
        page || this.queryParams.page + 1,
        limit
      ).then(({ data }) => {
        this.list = data.records;
        this.queryParams.total = data.total;
        this.queryParams.page = data.current;
        this.queryParams.limit = data.size;
      });
    },
    updateStatus(item, status) {
      addOrEditCoupon({
        ...item,
        status,
      }).then((res) => {
        this.loading = false;
        this.$message.success("操作成功");
        this.getListData(this.queryParams);
      });
    },
    deleteItem(id) {
      deleteUserCoupon(id).then((res) => {
        this.$message.success("删除成功");
        this.getListData(this.queryParams);
      });
    },
    create() {
      this.$router.push({ path: "/coupon/edit" });
    },
    edit(id) {
      this.$router.push({ path: `/coupon/edit?id=${id}` });
    },
    reset() {
      this.formData = {
        customerName: "",
        customerPhone: "",
        couponId: "",
        status: "",
      };
    },
    onSearch() {
      this.queryParams = {
        ...this.queryParams,
        ...this.formData,
      };
      this.getListData({
        page: 1,
        limit: 10,
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.create {
  margin-bottom: 20px;
}
</style>
